//
//  YYLVisitorView.swift
//  XinlangWeibo
//
//  Created by yangyinglei on 2017/3/27.
//  Copyright © 2017年 yangyinglei. All rights reserved.
//

import UIKit
import SnapKit

//自定义访客视图
class YYLVisitorView: UIView {
    //定义闭包
    var callBack:(()->())?
    //重写init方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        backgroundColor = UIColor.init(white: 237/255.0, alpha: 1)
        //添加控件
        addSubview(cycleImage)
        addSubview(maskImage)
        addSubview(homeImage)
        addSubview(txtLabel)
        addSubview(registBtn)
        addSubview(loginBtn)
        //布局
        cycleImage.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImage.snp.makeConstraints { (make) in
            make.center.equalTo(cycleImage)
        }
        homeImage.snp.makeConstraints { (make) in
            make.center.equalTo(maskImage)
        }
        
        txtLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(maskImage)
            make.top.equalTo(cycleImage.snp.bottom).offset(10)
            make.width.equalTo(224)
        }
        registBtn.snp.makeConstraints { (make) in
            make.leading.equalTo(txtLabel)
            make.top.equalTo(txtLabel.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 80, height: 30))
        }
        
        loginBtn.snp.makeConstraints { (make) in
            make.trailing.equalTo(txtLabel)
            make.top.equalTo(txtLabel.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 80, height: 30))
        }

    }
    
    //  懒加载圆形图片
    private lazy var cycleImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //  罩层
    private lazy var maskImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //  主页房子视图
    private lazy var homeImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //  文字=描述
    private lazy var txtLabel: UILabel = {
    let lable = UILabel()
        lable.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lable.font = UIFont.systemFont(ofSize: 13)
        lable.numberOfLines = 0
        //文字居中对齐
        lable.textAlignment = .center
        lable.textColor = UIColor.darkGray
        
        return lable
    }()
    //  注册按钮
    private lazy var registBtn: UIButton = {
    let btn = UIButton()
        btn.setTitle("注册", for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        //切背景图
        var backImg: UIImage = UIImage(named: "common_button_white_disable")!
//        backImg = backImg.resizableImage(withCapInsets: UIEdgeInsetsMake(1, 1, 1, 1), resizingMode: .stretch) 
        btn.setBackgroundImage(backImg, for: .normal)
        btn.adjustsImageWhenHighlighted = false
        
        btn.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        
        return btn
    }()
    
    //  登录按钮
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        //切背景图
        var backImg: UIImage = UIImage(named: "common_button_white_disable")!
//        backImg = backImg.resizableImage(withCapInsets: UIEdgeInsetsMake(1, 1, 1, 1), resizingMode: .stretch)
        btn.setBackgroundImage(backImg, for: .normal)
        btn.adjustsImageWhenHighlighted = false
        
        btn.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        
        return btn
    }()
    
    //  设置动画
    private func startAnimation() {
        //  创建基本动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //  目的地
        animation.toValue = 2 * M_PI
        //  动画时长
        animation.duration = 20
        //  动画执行次数
        animation.repeatCount = MAXFLOAT
        //  动画失去焦点会释放，设置动画在执行次数内不释放
        animation.isRemovedOnCompletion = false
        //  执行
        cycleImage.layer.add(animation, forKey: nil)
    
    }
    //  设置页面的展示的图片和文字
    func updateVisitorInfo(imgName: String?,txt: String?) {
        if let imgN = imgName, let msg = txt {
            //代码到这里表示不是首页
            homeImage.image = UIImage(named: imgN)
            txtLabel.text = msg
            cycleImage.isHidden = true
        } else {
        //这里是首页
            startAnimation()
        }
    }
    
    //登录动作
    @objc private func loginAction() {
    callBack?()
    
    }
}
